/*
 *   Copyright©2023 JoyCastle.
 *   All Rights Reserved.
 *   
 *   FileName:     UIShopManager.cs
 *   Author:       Administrator
 *   CreateTime:   2024/04/29 16:46:40
 *   UnityVersion: 2020.3.48f1c1
 *   Version:      1.1.9
 *   Description:
 *   
 */

using System;
using System.Collections;
using System.Collections.Generic;
using SevenPieceGame;
using Newtonsoft.Json;
using UnityEngine;

public class UIShopManager : MonoSingleton<UIShopManager>
{
    public enum PaypalAndCashingItemType
    {
        phase_1 = 0,
        phase_2 = 1,
        phase_3 = 2,
        phase_4 = 3,
        phase_5 = 4
    }
    //属性=============================================================================
    private List<ShopListItemData> m_ShopListItemDataList;
    
    //数据存储==========================================================================
    private const string SHOP_LIST_DATA_ITEM_1 = "Shop_List_Data_Item_1";
    private const string SHOP_LIST_DATA_ITEM_2 = "Shop_List_Data_Item_2";
    private const string SHOP_LIST_DATA_ITEM_3 = "Shop_List_Data_Item_3";
    private const string SHOP_LIST_DATA_ITEM_4 = "Shop_List_Data_Item_4";
    private const string SHOP_LIST_DATA_ITEM_5 = "Shop_List_Data_Item_5";
    private const string SHOP_WITH_DRAW_YOURNAME = "Shop_With_Draw_YourName";
    private const string SHOP_WITH_DRAW_EMAIL = "Shop_With_Draw_Email";
    private const string SHOP_WITH_DRAW_ACCOUNT = "Shop_With_Draw_Account";
    private const string SHOP_WITH_DRAW_CHANNEL = "Shop_With_Draw_Channel";
    //新手呼吸灯
    private const string SHOP_LIST_GUIDE_SHOW = "Shop_List_Guide_Show";
    //业务逻辑============================================================================
    public List<ShopListItemData> GetShopListItemData()
    {
        if (m_ShopListItemDataList == null)
        {
            InitShopListData();
        }
        return m_ShopListItemDataList;
    }
    
    public ShopListItemData GetShoListItemDataByIndex(int index)
    {
        if (m_ShopListItemDataList == null)
        {
            InitShopListData();
        }
        return m_ShopListItemDataList[index];
    }
    
    private void InitShopListData()
    {
        m_ShopListItemDataList = new List<ShopListItemData>();
        m_ShopListItemDataList.Add(InitShopListItemData(1));
        m_ShopListItemDataList.Add(InitShopListItemData(2));
        m_ShopListItemDataList.Add(InitShopListItemData(3));
        m_ShopListItemDataList.Add(InitShopListItemData(4));
        m_ShopListItemDataList.Add(InitShopListItemData(5));
        SaveShopListData();
    }

    //播放广告后
    public void AddRewardAdConut()
    {
        for (int i = 0; i < GetShopListItemData().Count; i++)
        {
            if (m_ShopListItemDataList[i].m_IsPaying == 1)
            {
                if (m_ShopListItemDataList[i].m_CMCountProgress < 50)
                {
                    m_ShopListItemDataList[i].m_CMCountProgress++;
                }
            }
            //是否有第五阶段后的广告播放
            if (m_ShopListItemDataList[i].m_ItemState == 4)
            {
                if (m_ShopListItemDataList[i].m_Phase5CMCount < 50)
                {
                    m_ShopListItemDataList[i].m_Phase5CMCount++;
                }
            }
        }
        SaveShopListData();
    }
    
    private void SaveShopListData()
    {
        PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_1, JsonUtility.ToJson(m_ShopListItemDataList[0]));
        PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_2, JsonUtility.ToJson(m_ShopListItemDataList[1]));
        PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_3, JsonUtility.ToJson(m_ShopListItemDataList[2]));
        PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_4, JsonUtility.ToJson(m_ShopListItemDataList[3]));
        PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_5, JsonUtility.ToJson(m_ShopListItemDataList[4]));
    }

    public void SaveShopListDataItem(int index)
    {
        switch (index)
        {
            case 1:
            {
                PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_1, JsonUtility.ToJson(m_ShopListItemDataList[0]));
                break;
            }
            case 2:
            {
                PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_2, JsonUtility.ToJson(m_ShopListItemDataList[1]));
                break;
           }
            case 3:
            {
                PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_3, JsonUtility.ToJson(m_ShopListItemDataList[2]));
                break;
            }
            case 4:
            {
                PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_4, JsonUtility.ToJson(m_ShopListItemDataList[3]));
                break;
            }
            case 5:
            {
                PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_5, JsonUtility.ToJson(m_ShopListItemDataList[4]));
                break;
            }
        }
    }
    
    //数据存储逻辑=========================================================================
    private void SaveShopListDataItem(ShopListItemData itemData)
    {
        switch (itemData.m_Index)
        {
            case 1:
            {
                PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_1, JsonUtility.ToJson(itemData));
                break;
            }
            case 2:
            {
                PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_2, JsonUtility.ToJson(itemData));
                break;
            }
            case 3:
            {
                PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_3, JsonUtility.ToJson(itemData));
                break;
            }
            case 4:
            {
                PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_4, JsonUtility.ToJson(itemData));
                break;
            }
            case 5:
            {
                PlayerPrefs.SetString(SHOP_LIST_DATA_ITEM_5, JsonUtility.ToJson(itemData));
                break;
            }
        }
    }

    private ShopListItemData GetShopListDataItem(int index)
    {
        string _itemDataStr = "";
        switch (index)
        {
            case 1:
            {
                _itemDataStr = PlayerPrefs.GetString(SHOP_LIST_DATA_ITEM_1,"");
                break;
            }
            case 2:
            {
                _itemDataStr = PlayerPrefs.GetString(SHOP_LIST_DATA_ITEM_2,"");
                break;
            }
            case 3:
            {
                _itemDataStr = PlayerPrefs.GetString(SHOP_LIST_DATA_ITEM_3,"");
                break;
            }
            case 4:
            {
                _itemDataStr = PlayerPrefs.GetString(SHOP_LIST_DATA_ITEM_4,"");
                break;
            }
            case 5:
            {
                _itemDataStr = PlayerPrefs.GetString(SHOP_LIST_DATA_ITEM_5,"");
                break;
            }
        }
        if (_itemDataStr.Equals(""))
        {
            ShopListItemData _data = InitShopListItemData(index);
            SaveShopListDataItem(_data);
            return _data;
        }
        else
        {
            return JsonUtility.FromJson<ShopListItemData>(_itemDataStr);
        }
    }

    public bool CheckCashChangeRedPoint()
    {
        int _curHaveDoller = PlayerPrefsManager.GetInt(PlayerPrefsManager.PLAYER_HISTROY_DOLLER_COUNT);
        for (int i = 1; i <= 5; i++)
        {
            ShopListItemData _data = GetShopListDataItem(i);
            if (_data.m_ItemState == 0)
            {
                //未进行购买 查看是否有相应的doller
                int _needDoller = i * 1000;
                if (_curHaveDoller >= _needDoller)
                {
                    return true;
                }
            }
        }
        return false;
    }
    
    public ShopListItemData GetShowItemData()
    {
        List<ShopListItemData> _shopListItemDatas = GetShopListItemData();
        ShopListItemData _tempData = null;
        for (int i = 0; i < _shopListItemDatas.Count; i++)
        {
            if (_shopListItemDatas[i].m_IsPaying != 0)
            {
                if (_shopListItemDatas[i].m_ItemState == (int)PaypalAndCashingItemType.phase_1)
                {
                    if (_tempData != null)
                    {
                        if (_shopListItemDatas[i].m_CMCountProgress < 50)
                        {
                            long _tempCMCount = 0;
                            long _CMCount = 0;
                            if (_tempData.m_ItemState == (int)PaypalAndCashingItemType.phase_1)
                            {
                                _tempCMCount = _tempData.m_TimeStamp;
                            }

                            if (_shopListItemDatas[i].m_ItemState == (int)PaypalAndCashingItemType.phase_1)
                            {
                                _CMCount = _tempData.m_TimeStamp;
                            }

                            if (_tempCMCount > _CMCount)
                            {
                                _tempData = _shopListItemDatas[i];
                            }   
                        }
                    }
                    else
                    {
                        if (_shopListItemDatas[i].m_CMCountProgress < 50)
                        {
                            _tempData = _shopListItemDatas[i];
                        }
                    }
                }
            }
        }

        return _tempData;
    }
    
    private ShopListItemData InitShopListItemData(int index)
    {
        string _itemDataStr = "";
        switch (index)
        {
            case 1:
            {
                _itemDataStr = PlayerPrefs.GetString(SHOP_LIST_DATA_ITEM_1,"");
                break;
            }
            case 2:
            {
                _itemDataStr = PlayerPrefs.GetString(SHOP_LIST_DATA_ITEM_2,"");
                break;
            }
            case 3:
            {
                _itemDataStr = PlayerPrefs.GetString(SHOP_LIST_DATA_ITEM_3,"");
                break;
            }
            case 4:
            {
                _itemDataStr = PlayerPrefs.GetString(SHOP_LIST_DATA_ITEM_4,"");
                break;
            }
            case 5:
            {
                _itemDataStr = PlayerPrefs.GetString(SHOP_LIST_DATA_ITEM_5,"");
                break;
            }
        }
        
        ShopListItemData _data = new ShopListItemData();
        if (_itemDataStr.Equals(""))
        {
            _data.m_Index = index;
            _data.m_IsPaying = 0;
            _data.m_ItemState = 0;
            _data.m_TimeStamp = 0;
            _data.m_CMCountProgress = 0;
            _data.m_Phase5CMCount = 0;
            _data.m_IsRestore = 0;
            DateTime now = DateTime.Now;
            string timestampString = now.Ticks.ToString();
            string lastEightDigits = timestampString.Substring(timestampString.Length - 8);
            _data.m_OrderNumber = $"BINGO{lastEightDigits}";
        }
        else
        {
            _data = JsonConvert.DeserializeObject<ShopListItemData>(_itemDataStr);
        }

        return _data;
    }

    public void SaveWithDrawData(string yourName, string email, string account)
    {
        PlayerPrefs.SetString(SHOP_WITH_DRAW_YOURNAME,yourName);
        PlayerPrefs.SetString(SHOP_WITH_DRAW_EMAIL,email);
        PlayerPrefs.SetString(SHOP_WITH_DRAW_ACCOUNT,account);
    }

    public string GetWithDrawYourName()
    {
        return PlayerPrefs.GetString(SHOP_WITH_DRAW_YOURNAME,"");
    }
    
    public string GetWithDrawEmail()
    {
        return PlayerPrefs.GetString(SHOP_WITH_DRAW_EMAIL,"");
    }
    
    public string GetWithDrawAccount()
    {
        return PlayerPrefs.GetString(SHOP_WITH_DRAW_ACCOUNT,"");
    }

    /// <summary>
    /// 0是cashapp 1是paypal
    /// </summary>
    /// <param name="channel"></param>
    public void SaveShopWithDrawChannel(int channel)
    {
        PlayerPrefs.SetInt(SHOP_WITH_DRAW_CHANNEL,channel);
    }
    
    public int GetShopWithDrawChannel()
    {
        return PlayerPrefs.GetInt(SHOP_WITH_DRAW_CHANNEL,1);
    }
    /// <summary>
    /// 1为已经展示过 2为未展示过
    /// </summary>
    public void SaveShopListGuideShow()
    {
        PlayerPrefs.SetInt(SHOP_LIST_GUIDE_SHOW,1);
    }

    public int GetShopListGuideShow()
    {
        return PlayerPrefs.GetInt(SHOP_LIST_GUIDE_SHOW,0);
    }
}

public class ShopListItemData
{
    public int m_Index; //序号
    public int m_IsPaying;//是否处于兑换状态 0为没有兑换 1是兑换状态
    public int m_ItemState;//进度状态
    //第一状态用数据
    public long m_TimeStamp; //时间戳
    public int m_CMCountProgress;//激励视频次数进度
    public int m_Phase5CMCount;//
    public string m_OrderNumber;
    public int m_IsRestore;//是否重复购买 0没有 1有
}

